//
//  IOTCContactsList.m
//  Contacts
//
//  Created by User-16 on 3/14/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "IOTCTaskList.h"
#import "IOTCTask.h"

@interface IOTCTaskList ()

@property (retain, nonatomic) NSMutableArray* tasks;

@end

@implementation IOTCTaskList

static BOOL isInited = NO;

+(id)sharedInstance
{
    static IOTCTaskList* instance = nil;
    if ( !instance ) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[super alloc] init];
        });
    }
    return instance;
}

+(id)alloc {
    return [IOTCTaskList sharedInstance];
}

-(id)copy {
    return [IOTCTaskList sharedInstance];
}

-(id)mutableCopy
{
    return  self;
}


- (id)init
{
    if (!isInited) {
        self = [super init];
        if (self) {
            
            //can't do lazy loading because we have to set notifications
            //loaddata
            self.tasks = [NSMutableArray array];
            
            [self.tasks addObject:[[IOTCTask alloc] initWithTaskDescription:@"Do Test 3"]];
            [[self.tasks objectAtIndex:0] finish];
            [self.tasks addObject:[[IOTCTask alloc] initWithTaskDescription:@"Clean up"]];
            [self.tasks addObject:[[IOTCTask alloc] initWithTaskDescription:@"Do the reading assignment"]];
            
            //set notifications if needed
            for(IOTCTask* task in self.tasks)
            {
                if(!task.isDone && task.endDate)
                    [task setLocalNotification];
            }
        }
        isInited = YES;
    }
    return self;
}


-(NSUInteger) numberOfTasks
{
    return [self.tasks count];
}

-(IOTCTask*) taskAtIndex:(NSUInteger) index
{
    return [self.tasks objectAtIndex:index];
}

-(void) removeTaskAtIndex:(NSUInteger) index
{
    [self.tasks removeObjectAtIndex:index];
}

-(void) addTask:(IOTCTask*) contact
{
    [self.tasks addObject:contact];
}

-(void) moveTaskAtIndex:(NSUInteger) fromIndex toIndex:(NSUInteger) toIndex
{
    IOTCTask* contact = [self.tasks objectAtIndex:fromIndex];
    [self.tasks removeObjectAtIndex: fromIndex];
    [self.tasks insertObject:contact atIndex:toIndex];
}

@end
